package cn.zwxky.android.user;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.ColorStateList;
import android.graphics.Rect;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;

import com.sobot.chat.SobotApi;
import com.sobot.chat.utils.ZhiChiConstant;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.LsfbAppManager;
import com.zgscwjm.lsfbbasetemplate.LsfbFragmentActivity;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;
import com.zgscwjm.lsfbbasetemplate.view.bottombar.BottomBarItem;

import java.util.ArrayList;
import java.util.List;

import cn.zwxky.android.user.HomePage.HomePageFragment;
import cn.zwxky.android.user.My.MyFragment;
import cn.zwxky.android.user.News.NewsFragment;
import cn.zwxky.android.user.Scope.ScopeFrament;
import cn.zwxky.android.user.Search.SearchFragment;
import cn.zwxky.android.user.Utils.LittleUtils;

/**
 * Created by Administrator on 2017/3/15 0015.
 */

public class IndexActivity extends LsfbFragmentActivity {
    private BottomBarItem bar1, bar2, bar3, bar4, bar5;
    private List<android.support.v4.app.Fragment> mFragments;
    private MyReceiver receiver;//广播

    @Override
    public void initView() {

        setIsScroll(false);
        LsfbEvent.getInstantiation().register2(this);
        ColorStateList cls = getResources().getColorStateList(R.color.index_bottom_btn);
        Rect rect = new Rect(0, 0, DensityUtils.dp2px(this, 20), DensityUtils.dp2px(this, 20));

        ArrayList list = new ArrayList<BottomBarItem>();

        bar1 = new BottomBarItem();
        bar1.setId(View.generateViewId());
        bar1.setColorStateList(cls);
        bar1.setName("业务查询");
        bar1.setImgRes(R.drawable.bottom_query);
        list.add(bar1);

        bar2 = new BottomBarItem();
        bar2.setId(View.generateViewId());
        bar2.setName("业务范围");
        bar2.setColorStateList(cls);
        bar2.setImgRes(R.drawable.bottom_scope);
        list.add(bar2);

        bar3 = new BottomBarItem();
        bar3.setId(View.generateViewId());
        bar3.setName("首页");
        bar1.setIsCheck(true);
        bar3.setColorStateList(cls);
        bar3.setImgRes(R.drawable.bottom_scope);
        list.add(bar3);

        bar4 = new BottomBarItem();
        bar4.setId(View.generateViewId());
        bar4.setName("新闻资讯");
        bar4.setColorStateList(cls);
        bar4.setImgRes(R.drawable.bottom_news);
        list.add(bar4);

        bar5 = new BottomBarItem();
        bar5.setId(View.generateViewId());
        bar5.setName("会员中心");
        bar5.setColorStateList(cls);
        bar5.setImgRes(R.drawable.bottom_my);
        list.add(bar5);

        bar1.setBounds(rect);
        bar2.setBounds(rect);
        bar3.setBounds(rect);
        bar4.setBounds(rect);
        bar5.setBounds(rect);

        getButtonBar().setPadding(0, 15, 0, 15);
//        getButtonBar().setRadioGroupHeight(DensityUtils.dp2px(this,80));
//        getButtonBar().setBackgroundColor(getResources().getColor(R.color.label_red));
        getButtonBar().initButtonBar(list, R.color.white_color);
        mFragments = new ArrayList<>();
        setMainBackground();
        initFragments();
        regReceiver();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    private void regReceiver() {
        //注册广播获取新收到的信息和未读消息数
        if (receiver == null) {
            receiver = new MyReceiver();
        }

        IntentFilter filter = new IntentFilter();
        filter.addAction(ZhiChiConstant.sobot_unreadCountBrocast);
        registerReceiver(receiver, filter);
    }

    //设置广播获取新收到的信息和未读消息数
    class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            int noReadNum = intent.getIntExtra("noReadCount", 0);
            String content = intent.getStringExtra("content");
            LittleUtils.badg=String.valueOf(noReadNum);
            //新消息内容
            LsfbLog.e(noReadNum+"新消息内容:" + content);
        }
    }

    /**
     * Fragment添加内容
     */
    public void initFragments() {
        //业务查询
        mFragments.add(new SearchFragment());
        //业务范围
        mFragments.add(new ScopeFrament());
        //首页
        mFragments.add(new HomePageFragment());
        //新闻资讯
        mFragments.add(new NewsFragment());
        //会员中心
        mFragments.add(new MyFragment());

        setTabFragments(mFragments);
        viewPager.setCurrentItem(2);
        getButtonBar().setCheckById(bar3.getId());
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void tabOnClick(View view, ViewPager viewPager) {
        int id = view.getId();
        if (id == bar1.getId()) {
            viewPager.setCurrentItem(0);
        } else if (id == bar2.getId()) {
            viewPager.setCurrentItem(1);
        }else if (id == bar3.getId()) {
            viewPager.setCurrentItem(2);
        }else if (id == bar4.getId()) {
            viewPager.setCurrentItem(3);
        }else if (id == bar5.getId()) {
            viewPager.setCurrentItem(4);
        }
    }

    private void initbarset() {
        switch (viewPager.getCurrentItem()) {
            case 0:
                getButtonBar().setCheckById(bar1.getId());
                break;
            case 1:
                getButtonBar().setCheckById(bar2.getId());
                break;
            case 2:
                getButtonBar().setCheckById(bar3.getId());
                break;
            case 3:
                getButtonBar().setCheckById(bar4.getId());
                break;
            case 4:
                getButtonBar().setCheckById(bar5.getId());
                break;
            default:
                break;
        }
    }

    //点击退出弹出确认提示框
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            dialog();
            return true;
        }
        return true;
    }

    protected void dialog() {

//    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//    设置Title的图标
//        builder.setIcon(R.mipmap.ic_launcher);
//    设置Title的内容
        builder.setTitle("温馨提示");
//    设置Content来显示一个信息
        builder.setMessage("确定要退出应用吗？");
//    设置一个PositiveButton
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
                SobotApi.exitSobotChat(IndexActivity.this);
                LsfbAppManager.getAppManager().finishAllActivity();
                System.exit(0);
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
//    设置一个NegativeButton
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
//    显示出该对话框
        builder.show();
    }


}